<template>
  <div>
    <div style="overflow: auto; " ref="content">
      <div v-if="player.level == 2" style="display: flex; flex-direction: column; justify-content: center;">
        <div style="display: flex; justify-content: space-between; align-items: center; padding: .5rem;">
          <span class="field-key" style="padding: 0;overflow: hidden;word-wrap: break-word;">{{player.name}}<span style="color: #FF5858; font-size: 1rem;">蜂鸣中...</span></span>
          <button  class="control-btn" style="background-color: #ff5858;" @click="closeProcessingClock">
            <svg t="1651212448187" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2862" width="30" height="30"><path d="M512 42.666667c259.2 0 469.333333 210.133333 469.333333 469.333333s-210.133333 469.333333-469.333333 469.333333S42.666667 771.2 42.666667 512 252.8 42.666667 512 42.666667z m0 64C288.149333 106.666667 106.666667 288.149333 106.666667 512s181.482667 405.333333 405.333333 405.333333 405.333333-181.482667 405.333333-405.333333S735.850667 106.666667 512 106.666667z m-124.608 219.178666a32 32 0 1 1 35.626667 53.141334 160 160 0 1 0 178.197333 0.170666 32 32 0 0 1 35.733333-53.077333A223.786667 223.786667 0 0 1 736 512c0 123.712-100.288 224-224 224S288 635.712 288 512c0-75.605333 37.76-144.853333 99.392-186.154667zM512 266.666667a32 32 0 0 1 31.850667 28.928L544 298.666667v149.333333a32 32 0 0 1-63.850667 3.072L480 448v-149.333333a32 32 0 0 1 32-32z" fill="#ffffff" p-id="2863"></path></svg>
          </button>
        </div>
      </div>
      <div class="clock-title" style="color: #888;" v-if="nextClockInfo != null && nextClockInfo.length > 0">
        {{nextClockInfo}}
      </div>
      <div v-for="(data, index) in list" :key="data">
        <div class="clock-title list-double-item">
          <div style="width:100%;overflow: hidden;word-wrap: break-word;">
            <span class="title-text" tabindex="0" :style="'color:'+ (data.clkStatus == 1 && data.audios && data.audios.length > 0 ? '#00c585' : '#ff0081')"  hidefocus="true" contenteditable="true" @blur="updateTitle($event, data,index)">{{data.clkName}}</span>
          </div>
          <div v-if="data.id" style="display: flex; flex-direction: row; align-items: center;">
            <switchBtn :ref="'clockSwitchInput' + data.id" class="clock-switch" height="1.3rem" width="2rem" :enabled="data.clkSwitch == 1 ? 1 : 2" @on-switch="e=>onOffClock(data,e)"/>
            <Button class="del-btn" @click="deleteClock(data, index)">
              <svg t="1650695525843" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4099" width="20" height="20"><path d="M989.866667 238.933333A102.4 102.4 0 0 0 887.466667 136.533333h-107.861334v-34.133333A102.4 102.4 0 0 0 677.205333 0h-341.333333a102.4 102.4 0 0 0-102.4 102.4V136.533333H136.533333A102.4 102.4 0 0 0 102.4 335.189333V887.466667a136.533333 136.533333 0 0 0 136.533333 136.533333h546.133334a136.533333 136.533333 0 0 0 136.533333-136.533333V335.189333a102.4 102.4 0 0 0 68.266667-96.256z m-688.128-136.533333a34.133333 34.133333 0 0 1 34.133333-34.133333h341.333333a34.133333 34.133333 0 0 1 34.133334 34.133333V136.533333h-409.6zM853.333333 887.466667a68.266667 68.266667 0 0 1-68.266666 68.266666H238.933333a68.266667 68.266667 0 0 1-68.266666-68.266666V341.333333h682.666666z m34.133334-614.4h-750.933334a34.133333 34.133333 0 0 1 0-68.266667h750.933334a34.133333 34.133333 0 0 1 0 68.266667z" fill="#d81e06" p-id="4100"></path><path d="M341.333333 836.266667a34.133333 34.133333 0 0 0 34.133334-34.133334v-307.2a34.133333 34.133333 0 0 0-68.266667 0v307.2a34.133333 34.133333 0 0 0 34.133333 34.133334zM512 836.266667a34.133333 34.133333 0 0 0 34.133333-34.133334v-307.2a34.133333 34.133333 0 0 0-68.266666 0v307.2a34.133333 34.133333 0 0 0 34.133333 34.133334zM682.666667 836.266667a34.133333 34.133333 0 0 0 34.133333-34.133334v-307.2a34.133333 34.133333 0 0 0-68.266667 0v307.2a34.133333 34.133333 0 0 0 34.133334 34.133334z" fill="#d81e06" p-id="4101"></path></svg>
            </Button>
          </div>
        </div>
        <div class="clock-content" v-if="data.id">
          <div class="list-double-item">
            <span class="field-key list-item-same-line"><span style="display: inline-block; background-color: #ff9191; color: #FFF; border-radius: 1rem; padding: 0 .3rem;" v-if="data.clkStatus != 1">失效</span>{{ data.timeDes }}</span>
            <Button class="del-btn" @click="openSetting(data, index)">
              <svg t="1650697544378" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5022" width="20" height="20"><path d="M512 697.6c102.4 0 182.4-83.2 182.4-185.6 0-102.4-83.2-185.6-182.4-185.6-102.4 0-182.4 83.2-182.4 185.6C329.6 614.4 412.8 697.6 512 697.6L512 697.6zM512 646.4c-73.6 0-134.4-60.8-134.4-134.4 0-73.6 60.8-134.4 134.4-134.4 73.6 0 134.4 60.8 134.4 134.4C646.4 585.6 585.6 646.4 512 646.4L512 646.4z" p-id="5023" fill="#1296db"></path><path d="M249.015232 843.178592c35.2 28.8 73.6 51.2 112 67.2 41.6-38.4 96-60.8 150.4-60.8 57.6 0 108.8 22.4 150.4 60.8 41.6-16 80-38.4 112-67.2-12.8-54.4-3.2-112 22.4-163.2 28.8-48 73.6-86.4 128-102.4 3.2-22.4 6.4-44.8 6.4-67.2 0-22.4-3.2-44.8-6.4-67.2-54.4-16-99.2-54.4-128-102.4-28.8-48-35.2-108.8-22.4-163.2-35.2-28.8-73.6-51.2-112-67.2-41.6 38.4-92.8 60.8-150.4 60.8-54.4 0-108.8-22.4-150.4-60.8-41.6 16-80 38.4-112 67.2 12.8 54.4 3.2 112-22.4 163.2-28.8 48-73.6 86.4-128 102.4-3.2 22.4-6.4 44.8-6.4 67.2 0 22.4 3.2 44.8 6.4 67.2 54.4 16 99.2 54.4 128 102.4C252.215232 731.178592 261.815232 788.778592 249.015232 843.178592M361.015232 958.378592c-54.4-19.2-105.6-48-150.4-89.6-6.4-6.4-9.6-16-6.4-22.4 16-48 9.6-99.2-16-140.8-25.6-44.8-64-73.6-112-83.2-9.6-3.2-16-9.6-16-19.2-6.4-28.8-9.6-60.8-9.6-89.6 0-28.8 3.2-57.6 9.6-89.6 3.2-9.6 9.6-16 16-19.2 48-12.8 89.6-41.6 112-83.2 25.6-44.8 28.8-92.8 16-140.8-3.2-9.6 0-19.2 6.4-22.4 44.8-38.4 96-67.2 150.4-89.6 9.6-3.2 16 0 22.4 6.4 35.2 35.2 80 57.6 128 57.6 48 0 96-19.2 128-57.6 6.4-6.4 16-9.6 22.4-6.4 54.4 19.2 105.6 48 150.4 89.6 6.4 6.4 9.6 16 6.4 22.4-16 48-9.6 99.2 16 140.8 25.6 44.8 64 73.6 112 83.2 9.6 3.2 16 9.6 16 19.2 6.4 28.8 9.6 60.8 9.6 89.6 0 28.8-3.2 57.6-9.6 89.6-3.2 9.6-9.6 16-16 19.2-48 12.8-89.6 41.6-112 83.2-25.6 44.8-28.8 92.8-16 140.8 3.2 9.6 0 19.2-6.4 22.4-44.8 38.4-96 67.2-150.4 89.6-9.6 3.2-16 0-22.4-6.4-35.2-35.2-80-57.6-128-57.6-48 0-96 19.2-128 57.6-3.2 3.2-9.6 6.4-16 6.4C364.215232 958.378592 361.015232 958.378592 361.015232 958.378592z" p-id="5024" fill="#1296db"></path></svg>
            </Button>
          </div>
          <div class="list-double-item">
            <span class="field-key list-item-same-line">{{ data.audioDes }}</span>
            <Button class="del-btn" @click="openMusicSetting(data,index)">
              <svg t="1650697724801" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8070" width="20" height="20"><path d="M42.666667 106.666667a21.333333 21.333333 0 0 1 21.333333-21.333334h896a21.333333 21.333333 0 0 1 0 42.666667H64a21.333333 21.333333 0 0 1-21.333333-21.333333z m21.333333 405.333333h597.333333a21.333333 21.333333 0 0 0 0-42.666667H64a21.333333 21.333333 0 0 0 0 42.666667z m384 341.333333H64a21.333333 21.333333 0 0 0 0 42.666667h384a21.333333 21.333333 0 0 0 0-42.666667z m532.853333-324.513333c-4.5-20.773333-14.666667-50.513333-37.466666-74.266667-11.04-11.493333-23.64-20.093333-37.493334-25.606666-10.306667-7.126667-19.44-16.58-27.153333-28.133334-19.64-29.393333-24.373333-64.04-25.446667-82.08A21.333333 21.333333 0 0 0 810.666667 320v479.586667c-1.413333-1.02-2.846667-2-4.326667-3.006667-27.64-18.433333-64-28.58-102.34-28.58s-74.666667 10.146667-102.34 28.58c-14.253333 9.5-25.56 20.746667-33.613333 33.44-8.88 14-13.38 29.013333-13.38 44.666667s4.5 30.666667 13.38 44.666666c8.053333 12.666667 19.333333 23.94 33.613333 33.44C629.333333 971.186667 665.646667 981.333333 704 981.333333s74.666667-10.146667 102.34-28.58c14.253333-9.5 25.56-20.746667 33.613333-33.44 8.88-14 13.38-29.013333 13.38-44.666666V438a140.893333 140.893333 0 0 0 30.966667 27.82A21.18 21.18 0 0 0 888.666667 468c8.713333 3.2 16.773333 8.606667 23.953333 16.086667 16.733333 17.42 23.806667 41.146667 26.533333 53.733333a21.333333 21.333333 0 1 0 41.706667-9.026667z" fill="#1296db" p-id="8071"></path></svg>
            </Button>
          </div>
        </div>
      </div>
    </div>
    <clockUpdater :clock-data="settingClockData" v-if="clockSettingIndex >= 0" @update="id=>timeUpdateFinished(id)"/>
    <music-updater :clock-data="settingClockData" v-if="musicSettingIndex >= 0" @update="v=>musicUpdateFinished(v)"></music-updater>
  </div>
</template>

<style lang="css" scoped>
.list-double-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.list-item-same-line {
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  text-wrap: normal;
  word-wrap: normal;
  white-space: nowrap;
}

.clock-title {
  background-color:#FFF;
  box-shadow: 0 .3rem .5rem rgba(63, 60, 60, 0.2);
  font-weight: bold;
  padding: .5rem;
}
.clock-content {
  padding: .5rem;
  background: rgba(187, 176, 176, 0.1);
}
.title-text{
  display: block;
  user-focus: true;
  outline: none;
}
.clock-content table {
  width: 100%;
}
.field-key {
  font-size: .8rem;
  font-weight: bold;
  color: #8c8c8c;
  padding: .5rem 0;
}
input[type=text] {
  border-width:0 0 .2rem 0;
  border-color: #68cbff;
  outline: none;
  width: 100%;
}
.music-list>li {
  border-top: .01rem solid #EEE;
}
.music-list>li .li-box {
  padding: .3rem 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.add-btn {
  width: 21px;
  height:21px;
  border-radius: 21px;
  padding:0;
  border:none;
}
.del-btn {
  margin-left: .5rem;
  width: 21px;
  height:21px;
  border-radius: 21px;
  padding:0;
  border:none;
}
.control-btn {
  width: 30px;
  height: 30px;
  border-radius: 30px;
  padding: 0;
  border: none;
  outline: none;
}
</style>

<script>
import musicUpdater from '@/views/clock/MusicUpdater'
import switchBtn from '@/views/tools/SwitchBtn'
import clockUpdater from '@/views/clock/CreaterOrUpdater'
import request from '@/requestutil'
import {reactive} from "vue";
export default {
  name : "ClockPane",
  components: {
    musicUpdater,
    clockUpdater,
    switchBtn
  },
  data() {
    return {
      hourData: {},
      minuteData: {},
      list:reactive([]),

      settingClockData: {},
      clockSettingIndex: -1,

      musicSettingIndex: -1,
      nextClockInfo: '',
      player: {}
    }
  },
  mounted() {
    this.refreshList()
    if (!window.stateWatcher) {
      window.stateWatcher={}
    }
    window.stateWatcher.showClock = true
    let self = this
    window.stateWatcher.clockWatcher = function (){
      if (!window.stateWatcher.showClock) {
        return
      }
      let second = 60 - new Date().getSeconds()
      if (second === 0) {
        second = 60
      }
      second++
      let watcherId = setTimeout(()=>{
        if (window.stateWatcher.clockWatcherId === watcherId) {
          self.getNextClock()
          window.stateWatcher.clockWatcher()
        }
      }, second * 1000)
      window.stateWatcher.clockWatcherId = watcherId
    }
    window.stateWatcher.clockWatcher()

    window.stateWatcher.musicClockWatcher = function (overrideSecond){
      if (!window.stateWatcher.showClock) {
        return
      }
      let second
      if (overrideSecond !== undefined) {
        second = overrideSecond
      } else {
        second = 60 - new Date().getSeconds()
        if (second === 0) {
          second = 60
        }
        second ++
      }
      let musicId = setTimeout(()=>{
        if (window.stateWatcher.clockProcessWatcherId === musicId) {
          request.get('/api/music/current')
          .then(r=>{
            if (r.data.code == 1) {
              let clockInfo = r.data.data
              if (clockInfo && clockInfo.level == 2) {
                self.player.name = clockInfo.name
                self.player.level = clockInfo.level
                window.stateWatcher.musicClockWatcher(1)
                return
              }
            }
            if (self.player.level) {
              self.player.name = ''
              self.player.level = 0
            }
            window.stateWatcher.musicClockWatcher()
          })
          .catch(()=>{
            if (self.player.level) {
              self.player.name = ''
              self.player.level = 0
            }
            window.stateWatcher.musicClockWatcher()
          })
        }
      }, second <= 0 ? 10 : second * 1000)
      window.stateWatcher.clockProcessWatcherId = musicId
    }
    window.stateWatcher.musicClockWatcher(0)
  },
  beforeUnmount() {
    window.stateWatcher.showClock = false
    let clockWatcherId = window.stateWatcher.clockWatcherId
    if (clockWatcherId) {
      window.clearTimeout(clockWatcherId)
    }
    let clockProcessWatcherId = window.stateWatcher.clockProcessWatcherId
    if (clockProcessWatcherId) {
      window.clearTimeout(clockProcessWatcherId)
    }
  },
  methods: {
    refreshList() {
      request.get('/api/clock/list')
      .then(r=>{
        let data =r.data
        if (data.code == 1) {
          let dataList = data.data || []
          for (let i=0;i<dataList.length; i++) {
            dataList[i].timeDes = translateToTimeDes(dataList[i])
            dataList[i].audioDes = translateToAudioDes(dataList[i])
          }
          this.list = dataList
        }
        this.list.push(createNewTime())
      })
      this.getNextClock()
    },
    //关闭正在蜂鸣的闹铃
    closeProcessingClock() {
      request.get('/api/music/close/2')
      .then(r=>{
        if (r.data.code != 1) {
          window.openMsg('操作失败')
        }
      })
    },
    getNextClock() {
      let self = this
      refreshNextClock(r=>{
        let text = ''
        if (r.code == 1) {
          let next = r.data
          if (next) {
            let time = next.nextTime
            let clocks = next.clockInfos
            if (clocks && clocks.length > 0 && time) {
              let available = []
              for (let i=0; i < clocks.length; i++) {
                let c = clocks[i]
                if (c.audios && c.audios.length > 0) {
                  available.push(c)
                }
              }
              clocks = available
              if (clocks.length > 0) {
                let date = new Date(time)
                let now = new Date()
                text = next.clockInfos[0].clkName;
                if (clocks.length > 1) {
                  text += '等'+clocks.length+'个闹铃'
                }
                text +='将在'
                if (now.getFullYear() !== date.getFullYear()) {
                  text += date.getFullYear()+'年' + (date.getMonth() + 1) +'月' + date.getDate() + '号' + '的' + translate2(date.getHours())+'点'+translateMinute(date.getMinutes())
                } else if (now.getMonth() !== date.getMonth()) {
                  text += (date.getMonth() + 1) +'月' + date.getDate() + '号' + '的' + translate2(date.getHours())+'点'+translateMinute(date.getMinutes())
                } else if (now.getDate() !== date.getDate()) {
                  text += date.getDate() + '号' + '的' + translate2(date.getHours())+'点'+translateMinute(date.getMinutes())
                } else if (now.getHours() !== date.getHours()) {
                  text += translate2(date.getHours())+'点'+translateMinute(date.getMinutes())
                } else {
                  text += translateMinute(date.getMinutes())
                }
                var des = translateNextTimeDes(parseInt(date.getTime() / 1000) - parseInt(now.getTime() / 1000))
                if (des) text +='(' + des + ')'
                text +='响起'
              }
            }
          }
        }
        self.nextClockInfo = text
      })
    },
    onOffClock(data,state){
      console.log(this.$refs)
      let self = this
      let clkSwitch = state == 1 ? 1 : 0
      request.post('/api/clock/switch', {id: data.id, clkSwitch: clkSwitch})
      .then(r=>{
        if (r.data.code == 1) {
          data.clkSwitch = clkSwitch
          self.getNextClock()
          window.openMsg((state == 1 ? '打开' : '关闭') + '闹铃成功', true)
        } else {
          self.$refs['clockSwitchInput'+data.id][0].switchState(data.clkSwitch==1?1:2,true)
          window.openMsg('操作失败；'+r.data.msg)
        }
      })
    },
    deleteClock(data,index){
      let self = this
      doDeleteClock(request, data.id, (data)=>{
        if (data.code == 1) {
          this.list.splice(index,1)
          self.getNextClock()
          window.openMsg('删除成功', true)
        } else {
          window.openMsg(data.msg)
        }
      })
    },
    openSetting(data, index) {
      this.settingClockData = data
      this.clockSettingIndex = index
    },
    timeUpdateFinished: function (id) {
      if (id != 0) {
        let currIndex = this.clockSettingIndex
        refreshById(currIndex, this)
        this.getNextClock()
        this.clockSettingIndex = -1
      }
    },
    openMusicSetting(data, index) {
      this.settingClockData = data
      this.musicSettingIndex = index
    },
    musicUpdateFinished: function (id) {
      if (id != 0) {
        let currIndex = this.musicSettingIndex
        refreshById(currIndex, this)
        this.getNextClock()
        this.musicSettingIndex = -1
      }
    },
    updateTitle(event, data, index){
      let self = this
      let el = this.list[index]
      let text = event.currentTarget.innerText.trim()
      if (text !== el.clkName) {
        if (text.length > 0) {
          el.clkName = text
          createOrUpdate(request, {id:data.id, clkName: data.clkName}, data=>{
            if (data.code == 1) {
              if (!el.id) {
                this.list.push(createNewTime())
              }
              el.id = data.data
              self.getNextClock()
              window.openMsg('修改成功',true)
            } else {
              window.openMsg('修改失败：'+data.msg)
            }
          })
        }
      }
    }
  }
}

let weekText = ['一','二','三','四','五','六','日']
function translateToTimeDes(clock) {
  if (clock.clkMinute == null) {
    return '时钟未设置'
  }
  if (clock.clkType == 2) {
    var text = translateMinute(clock.clkMinute)
    text = (clock.clkHour == null ? '每小时' : translate2(clock.clkHour) + '点') + text
    if (clock.clkWeek != null) {
      text = '每周' + weekText[parseInt(clock.clkWeek) - 1] + text
    } else if (clock.clkDay != null) {
      text = clock.clkDay+'号的' + text
    } else {
      if (clock.clkHour != null) {
        text = '每天' + text
      }
    }
    if (clock.clkMonth != null) {
      text = clock.clkMonth + '月的' + text
    } else if (clock.clkDay != null){
      text = '每月' + text
    }

    if (clock.clkYear != null) {
      text = clock.clkYear + '年' + text
    } else if (clock.clkMonth != null) {
      text = '每年' + text
    }
  } else if (clock.clkType == 3 || clock.clkType == 4) {
    text = '法定'+ (clock.clkType == 3 ? '节假日' : '工作日')
    if (clock.holidayName) {
      text += '(' + clock.holidayName + ')'
    }
    if (clock.clkYear) {
      text += clock.clkYear + '年' + clock.clkMonth + '月' + clock.clkDay + '号的' + translate2(clock.clkHour) + '点' + translateMinute(clock.clkMinute)
    } else if (clock.clkHour != null) {
      text += translate2(clock.clkHour)+'点'+translateMinute(clock.clkMinute) + (clock.audios && clock.audios.length > 0 ? '(未获取到法定'+(clock.clkType == 3 ? '节假日' : '工作日')+'信息)' : '(未添加音乐)')
    }
  } else if (clock.clkType == 5 || clock.clkType == 6) {
    text = clock.clkType == 5 ? '母亲节' : '父亲节'
    if (clock.beforeDay) {
      text +='的前'+ translate2(clock.beforeDay)+'天'
    } else {
      text += '当天'
    }
    if (clock.clkYear) {
      text += '：'+clock.clkYear + '年' + clock.clkMonth + '月' + clock.clkDay + '号的' + translate2(clock.clkHour) + '点' + translateMinute(clock.clkMinute)
    }
  } else if (clock.clkType == 7) {
    text = '阴历生日'+clock.lunarMonth+'月'+clock.lunarDay
    if (clock.clkYear) {
      text += '('+clock.clkYear + '年' + clock.clkMonth + '月' + clock.clkDay + '号的' + translate2(clock.clkHour) + '点' + translateMinute(clock.clkMinute)+')'
    }
  } else {
    text = clock.clkYear + '年' + clock.clkMonth + '月' + clock.clkDay + '号的' + translate2(clock.clkHour) + '点' + translateMinute(clock.clkMinute)
    if (clock.clkType == 0) {
      text += '响一次'
    } else {
      text += '起，每隔' + clock.clkBetween + '分钟响一次'
    }
  }
  return text
}

function translateNextTimeDes(seconds) {
  if (seconds < 60) {
    return '一分钟内'
  }
  if (seconds < 3600) {
    return translate2(parseInt(seconds / 60)) +'分钟后'
  }
  if (seconds < 86400) {
    var hour = parseInt(seconds / 3600)
    var minute = parseInt(seconds % 3600 / 60)
    return translate2(hour) +'小时' + (minute == 0 ? '' : (minute < 10 ? '0' + minute : minute) + '分钟') +'后'
  }
}

function translate2(hour) {
  return hour == 2 ? '两' : hour;
}

function translateMinute(minute) {
  return minute < 10 ? (minute == 0 ? '整' : '0' + minute + '分') : minute + '分'
}

function translateToAudioDes(clock) {
  if (!clock.audios || clock.audios.length === 0) {
    return '没有音乐'
  }
  if (clock.audios.length === 1) {
    return clock.audios[0].audioName
  }
  return clock.audios[0].audioName + '等' + clock.audios.length +'个音乐'
}

function refreshById(currIndex, self) {
  refreshOne(request,self.settingClockData.id, data=>{
    if (data.code != 1) {
      window.openMsg('刷新失败：'+data.msg)
      return
    }
    data.data.timeDes=translateToTimeDes(data.data)
    data.data.audioDes=translateToAudioDes(data.data)
    self.list[currIndex] = data.data
  })
}

function createOrUpdate(request, param, callback) {
  request.post('/api/clock/createOrUpdate', param)
  .then(r=>{
    if (callback) callback(r.data)
  })
}

function doDeleteClock(request,id, callback) {
  request.post('/api/clock/deleteClock',{id:id})
  .then(r=>{
    if (callback) callback(r.data)
  })
}

function refreshOne(request, id, callback) {
  request.get('/api/clock/clock/'+id)
  .then(r=>{
    if (callback) callback(r.data)
  })
}

function refreshNextClock(callback) {
  request.get('/api/clock/next')
  .then(r=>{
    if (r.data.code != 1) {
      window.open('拉取闹铃信息失败')
    }
    if (callback) {
      callback(r.data)
    }
  }).catch(()=>{
    if (callback) {
      callback({code:0})
    }
  })
}

function createNewTime() {
  return {
    clkName: '待新增',
    timeDes: '时钟未设置',
    audioDes: '没有音乐',
    clkSwitch: 1,
    clkType: 0
  }
}
</script>